package com.charlygao.script;

import java.awt.event.KeyEvent;
import com.charlygao.Command;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.win32.W32APIOptions;

public class AutoCraft implements Runnable
{
	@Override
	public void run() 
	{
		try
		{
			User32 user32 = (User32) Native.loadLibrary("User32", User32.class, W32APIOptions.UNICODE_OPTIONS);
			Command command = new Command();
			HWND hwnd = user32.GetForegroundWindow();
			
			mainLoop:while(true)
			{
				Thread.sleep(1000);
				command.KeyPress(hwnd, KeyEvent.VK_F5);
				while(!command.isColor(hwnd, "fff0bf16", 890, 80))
					Thread.sleep(100);
				command.KeyPress(hwnd, KeyEvent.VK_F2);
				while(!command.isColor(hwnd, "ff050505", 559, 490))
					Thread.sleep(100);
				for (int i=0; i<1; i++)
					command.KeyPress(hwnd, KeyEvent.VK_DOWN);
				command.KeyPress(hwnd, KeyEvent.VK_ENTER);
				while(!command.isColor(hwnd, "ffc89b6b", 560, 448))
					Thread.sleep(100);
				Thread.sleep(1000);
				command.LeftClick(hwnd, 561, 448);
				Thread.sleep(10000);
				while(command.isColor(hwnd, "fff77cf7", 548, 397))
				{
					if (command.isColor(hwnd, "ffff8080", 487, 212) || command.isColor(hwnd, "ffff8080", 542, 211))
						break mainLoop;
					else
						Thread.sleep(1000);
				}
				command.LeftClick(hwnd, 750, 490);
				Thread.sleep(500);
				
				for (int i=0; i<4; i++)
				{
					command.LeftClick(hwnd, 871, 526);
					while(!command.isColor(hwnd, "ff9eb5c4", 709, 490))
						Thread.sleep(100);
					command.LeftClick(hwnd, 709, 490);
					Thread.sleep(1500);
				}
			}
			user32.SetForegroundWindow(hwnd);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}
